<?php
/*************************************************************************
Class Pick_Core_Modules
----------------------------------------------------------------
PICKCOCMS Project
Company: PICKCO  Co., Ltd 
Name: Nguyen Hai Nam                                    
Last updated: 14/10/2009
**************************************************************************/	
class Pick_Core_Modules extends Pick_Core_Objects {
	function Pick_Core_Modules($database = '') {
		if(!$database) {
			global $db;
			$this->_db = $db;
		} else $this->_db = $database;
		$this->objectInfo 	= 'Pick_Core_ModulesInfo';
		$this->table = DB_PREFIX."modules";
	}
/*-----------------------------------------------------------------------*
* Function: getObjects
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectFromCode($code) {
		$results = $this->getObjects(1, "code = '$code' OR slug = '$code'", array('code'=>'ASC'), 0, 1);
		if($results) {				
			return $results['data'][0];			
		}
		return;
	}
/*-----------------------------------------------------------------------*
* Function: getObjectsFromArrayIdForMenu
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromArrayIdForMenu($page = 1, $array_id = array(), $sort = array(), $items_per_page = 0) {
		if(count($array_id)>0){		
			$string_id = implode(',',$array_id);
			$result = $this->getObjects($page, 'id IN('.$string_id.')' . ' AND status = 1  AND ismenu = 1', $sort, $items_per_page);
			if($result) return $result['data']; 			
		}
		return;
	}
/*-----------------------------------------------------------------------*
* Function: getObjectsFromArrayIdForMenuConf
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromArrayIdForMenuConf($page = 1, $array_id = array(), $sort = array(), $items_per_page = 0) {
		if(count($array_id)>0){
			$string_id = implode(',',$array_id);
			return $this->getObjects($page, 'id IN('.$string_id.')' . ' AND status = 1  AND ismenu = 2', $sort, $items_per_page);			
		}
		return;
	}
/*-----------------------------------------------------------------------*
* Function: getObjectsFromArrayIdForMenuConf
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getArrayObjectsFromPermissionUser($objectUser, $cid = 0) {
		if(is_object($objectUser)){
			$arrayObjects = array();
			$objectCategorys = $this->getObjects(1, "status = 1 AND `cid` = '$cid' AND `typeid` = 1 AND (ismenu = 1 OR ismenu = 2)", array('position'=>'ASC'), '');
			if($objectCategorys) $objectCategorys = $objectCategorys['data'];
			if(count($objectCategorys)>0){
					foreach($objectCategorys as $object){
					
						$obj = array();
						$idmodule = $object->getField('id');
						$codemodule = $object->getField('code');
						if($objectUser->checkPermissionModule($codemodule)){
						
							$obj[0] = $this->getObject($idmodule);
							if(is_object($obj[0])){
							
								$obj[1] = $this->getArrayObjectsFromPermissionUser($objectUser, $idmodule);
								if(count($obj[1])){
									$arrayObjects[] = $obj;
								}else{
									$arrayObjects[] = $obj[0];
								}#endif
								
							}#endif
							
						}#endif
						
					}#endforeach
			}#endif
			return $arrayObjects;

		}else{
			exit("You haven't premission to access the manage website !");
		}
	}
/*-----------------------------------------------------------------------*
* Function: getSubCategory
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getSubCategory($cid) {
		$objSub = $this->getObjects(1, "`cid` = '$cid' AND status = 1", array('position'=>'ASC'));
		if($objSub) return $objSub['data'];
		return; 
	}
/*-----------------------------------------------------------------------*
* Function: getArrayModuleFromArrayId
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getArrayModuleFromArrayId($page = 1, $array_id = array(), $sort = array(), $items_per_page = 0) {
		if(count($array_id)>0){		
			if(!$page) $page = 1;
			$start = ($page -1) * $items_per_page;
			$string_id = implode(',',$array_id);
			$result = $this->getObjects(1, 'id IN('.$string_id.')' . ' AND status = 1', $sort, $start, $items_per_page);
			$arrayModule = array();
			if(is_array($result)){
				foreach($result['data'] as $object){
					$arrayModule[] = $object->getField('code');
				}#endforeach
				return $arrayModule;
			}#endif
		}
		return;
	}
/*-----------------------------------------------------------------------*
* Function: getArrayModuleFromArrayCode
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getArrayModuleFromArrayCode($page = 1, $array_id = array(), $sort = array(), $items_per_page = 0) {
		if(count($array_id)>0){
			if(!$page) $page = 1;
			$start = ($page -1) * $items_per_page;
			$string_id = implode('","',$array_id);
			$result = $this->getObjects(1, 'code IN("'.$string_id.'")' . ' AND status = 1', $sort, $start, $items_per_page);
			$arrayModule = array();
			if(is_array($result)){
				foreach($result['data'] as $object){
					$arrayModule[] = $object->getField('code');
				}#endforeach
				return $arrayModule;
			}#endif
		}
		return;
	}
/*-----------------------------------------------------------------------*
* Function: getOptionPI
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getOptionAllLevelModule($cid = 0, $value = '', $sort = array('position' => 'ASC'), $typeModule = 1, $level = 4, $pace = "|"){
        global $lang;
		$str = '&nbsp;';
		$objectCategory = $this->getObjects(1, "`cid` = $cid AND `typeid` = $typeModule AND status = 1", $sort, '');
		if($objectCategory) $objectCategory = $objectCategory['data'];
		if(count($objectCategory)>0 && $level>0):
			--$level;
			$pace .= "---";
			foreach($objectCategory as $object):
				$str .= "<option value='".$object->getField('id')."'".($object->getField('id')==$value?" selected=\"selected\"":"").">" . $pace . $object->getFieldLangAdmin($lang, 'name'). "</option>";
				$str .= $this->getOptionAllLevelModule($object->getField('id'), $value, $sort, $typeModule, $level, $pace);
			endforeach;
		endif;
		return $str;
	}				
}

?>
